package com.angel.architecture.persistence.beans;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Embedded;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;

import com.angel.architecture.persistence.base.PersistentObject;
import com.angel.common.helpers.StringHelper;

@MappedSuperclass
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "userType", discriminatorType=DiscriminatorType.STRING, length=10)
public abstract class User extends PersistentObject implements Serializable {

	private static final long serialVersionUID = 4620634764452916408L;

	@Column(nullable=false, unique=true)
	private String name;

	@Column(nullable=false)
	private String password;

	@Column(nullable=false, unique=true)
	private String email;

    private Boolean active = true;

	@Embedded
	private UserRoles roles = new UserRoles(this);

	public User(){
        super();
	}

	public User(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = StringHelper.hash(password);
	}

	public String getEmail() {
		return this.email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public UserRoles getRoles() {
		return this.roles;
	}

    public void addRole(String name, String description){
        Role role = new Role();
        role.setDescription(description);
        role.setName(name);
        this.addRole(role);
    }

    public void addRole(Role role){
        this.getRoles().addRole(role);
    }

    public Boolean getActive() {
        return active;
    }

    public void setActive(Boolean active) {
        this.active = active;
    }

    public void addAllRoles(Collection<Role> roles) {
        this.getRoles().getRoles().addAll(roles);
    }

    public void clearAllRoles() {
        this.getRoles().getRoles().clear();
    }
}
